home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / progjrn / pj_6_6.arc / 66L3.ASM < prev    next >
Assembly Source File  |  1988-08-17  |  6KB  |  257 lines

  1. ;
  2. ; *** Listing 3 ***
  3. ;
  4. ; Program to illustrate the color mapping capabilities of the
  5. ; EGA's palette registers.
  6. ;
  7. VGA_SEGMENT    equ    0a000h
  8. SC_INDEX    equ    3c4h    ;Sequence Controller Index register
  9. MAP_MASK    equ    2    ;Map Mask register index in SC
  10. BAR_HEIGHT    equ    14    ;height of each bar
  11. TOP_BAR        equ    BAR_HEIGHT*6    ;start the bars down a bit to
  12.                     ; leave room for text
  13. ;
  14. stack    segment para stack 'STACK'
  15.     db    512 dup (?)
  16. stack    ends
  17. ;
  18. Data    segment    word 'DATA'
  19. KeyMsg    db    'Press any key to see the next color set. '
  20.     db    'There are 64 color sets in all.'
  21.     db    0dh, 0ah, 0ah, 0ah, 0ah
  22.     db    13 dup (' '), 'Attribute'
  23.     db    38 dup (' '), 'Color$'
  24. ;
  25. ; Used to label the attributes of the color bars.
  26. ;
  27. AttributeNumbers    label    byte
  28. x=    0
  29.     rept    16
  30. if x lt 10
  31.     db    '0', x+'0', 'h', 0ah, 8, 8, 8
  32. else
  33.     db    '0', x+'A'-10, 'h', 0ah, 8, 8, 8
  34. endif
  35. x=    x+1
  36.     endm
  37.     db    '$'
  38. ;
  39. ; Used to label the colors of the color bars. (Color values are
  40. ; filled in on the fly.)
  41. ;
  42. ColorNumbers    label    byte
  43.     rept    16
  44.     db    '000h', 0ah, 8, 8, 8, 8
  45.     endm
  46. COLOR_ENTRY_LENGTH    equ    ($-ColorNumbers)/16
  47.     db    '$'
  48. ;
  49. CurrentColor    db    ?
  50. ;
  51. ; Space for the array of 16 colors we'll pass to the BIOS, plus
  52. ; an overscan setting of black.
  53. ;
  54. ColorTable    db    16 dup (?), 0
  55. Data    ends
  56. ;
  57. Code    segment
  58.     assume    cs:Code, ds:Data
  59. Start    proc    near
  60.     cld
  61.     mov    ax,Data
  62.     mov    ds,ax
  63. ;
  64. ; Go to hi-res graphics mode.
  65. ;
  66.     mov    ax,10h    ;AH = 0 means mode set, AL = 10h selects
  67.             ; hi-res graphics mode
  68.     int    10h    ;BIOS video interrupt
  69. ;
  70. ; Put up relevant text.
  71. ;
  72.     mov    ah,9    ;DOS print string function
  73.     mov    dx,offset KeyMsg
  74.     int    21h
  75. ;
  76. ; Put up the color bars, one in each of the 16 possible pixel values
  77. ; (which we'll call attributes).
  78. ;
  79.     mov    cx,16    ;we'll put up 16 color bars
  80.     sub    al,al    ;start with attribute 0
  81. BarLoop:
  82.     push    ax
  83.     push    cx
  84.     call    BarUp
  85.     pop    cx
  86.     pop    ax
  87.     inc    ax    ;select the next attribute
  88.     loop    BarLoop
  89. ;
  90. ; Put up the attribute labels.
  91. ;
  92.     mov    ah,2    ;video interrupt set cursor position function
  93.     sub    bh,bh    ;page 0
  94.     mov    dh,TOP_BAR/14    ;counting in character rows, match to
  95.                 ; top of first bar, counting in
  96.                 ; scan lines
  97.     mov    dl,16    ;just to left of bars
  98.     int    10h
  99.     mov    ah,9    ;DOS print string function
  100.     mov    dx,offset AttributeNumbers
  101.     int    21h
  102. ;
  103. ; Loop through the color set, one new setting per keypress.
  104. ;
  105.     mov    [CurrentColor],0    ;start with color zero
  106. ColorLoop:
  107. ;
  108. ; Set the palette registers to the current color set, consisting
  109. ; of the current color mapped to attribute 0, current color + 1
  110. ; mapped to attribute 1, and so on.
  111. ;
  112.     mov    al,[CurrentColor]
  113.     mov    bx,offset ColorTable
  114.     mov    cx,16    ;we have 16 colors to set
  115. PaletteSetLoop:
  116.     and    al,3fh        ;limit to 6-bit color values
  117.     mov    [bx],al    ;built the 16-color table used for setting
  118.     inc    bx    ; the palette registers
  119.     inc    ax
  120.     loop    PaletteSetLoop
  121.     mov    ah,10h    ;video interrupt palette function
  122.     mov    al,2    ;subfunction to set all 16 palette registers
  123.             ; and overscan at once
  124.     mov    dx,offset ColorTable
  125.     push    ds
  126.     pop    es    ;ES:DX points to the color table
  127.     int    10h    ;invoke the video interrupt to set the palette
  128. ;
  129. ; Put up the color numbers, so we can see how attributes map
  130. ; to color values, and so we can see how each color # looks
  131. ; (at least on this particular screen).
  132. ;
  133.     call    ColorNumbersUp
  134. ;
  135. ; Wait for a keypress, so they can see this color set.
  136. ;
  137. WaitKey:
  138.     mov    ah,8    ;DOS input without echo function
  139.     int    21h
  140. ;
  141. ; Advance to the next color set.
  142. ;
  143.     mov    al,[CurrentColor]
  144.     inc    ax
  145.     mov    [CurrentColor],al
  146.     cmp    al,64
  147.     jbe    ColorLoop
  148. ;
  149. ; Restore text mode.
  150. ;
  151.     mov    ax,3
  152.     int    10h
  153. ;
  154. ; Done.
  155. ;
  156. Done:
  157.     mov    ah,4ch    ;DOS terminate function
  158.     int    21h
  159. ;
  160. ; Puts up a bar consisting of the specified attribute (pixel value),
  161. ; at a vertical position corresponding to the attribute.
  162. ;
  163. ; Input: AL = attribute
  164. ;
  165. BarUp    proc    near
  166.     mov    dx,SC_INDEX
  167.     mov    ah,al
  168.     mov    al,MAP_MASK
  169.     out    dx,al
  170.     inc    dx
  171.     mov    al,ah
  172.     out    dx,al    ;set the Map Mask register to produce
  173.             ; the desired color
  174.     mov    ah,BAR_HEIGHT
  175.     mul    ah    ;row of top of bar
  176.     add    ax,TOP_BAR ;start a few lines down to leave room for
  177.                ; text
  178.     mov    dx,80    ;rows are 80 bytes long
  179.     mul    dx    ;offset in bytes of start of scan line bar
  180.             ; starts on
  181.     add    ax,20    ;offset in bytes of upper left corner of bar
  182.     mov    di,ax
  183.     mov    ax,VGA_SEGMENT
  184.     mov    es,ax    ;ES:DI points to offset of upper left
  185.             ; corner of bar
  186.     mov    dx,BAR_HEIGHT
  187.     mov    al,0ffh
  188. BarLineLoop:
  189.     mov    cx,40    ;make the bars 40 wide
  190.     rep    stosb    ;do one scan line of the bar
  191.     add    di,40    ;point to the start of the next scan line
  192.             ; of the bar
  193.     dec    dx
  194.     jnz    BarLineLoop
  195.     ret
  196. BarUp    endp
  197. ;
  198. ; Converts AL to a hex digit in the range 0-F.
  199. ;
  200. BinToHexDigit    proc    near
  201.     cmp    al,9
  202.     ja    IsHex
  203.     add    al,'0'
  204.     ret
  205. IsHex:
  206.     add    al,'A'-10
  207.     ret
  208. BinToHexDigit    endp
  209. ;
  210. ; Displays the color values generated by the color bars given the
  211. ; current palette register settings off to the right of the color
  212. ; bars.
  213. ;
  214. ColorNumbersUp    proc    near
  215.     mov    ah,2    ;video interrupt set cursor position function
  216.     sub    bh,bh    ;page 0
  217.     mov    dh,TOP_BAR/14    ;counting in character rows, match to
  218.                 ; top of first bar, counting in
  219.                 ; scan lines
  220.     mov    dl,20+40+1    ;just to right of bars
  221.     int    10h
  222.     mov    al,[CurrentColor]    ;start with the current color
  223.     mov    bx,offset ColorNumbers+1
  224.             ;build the color number text string on the fly
  225.     mov    cx,16    ;we've got 16 colors to do
  226. ColorNumberLoop:
  227.     push    ax        ;save the color #
  228.     and    al,3fh        ;limit to 6-bit color values
  229.     shr    al,1
  230.     shr    al,1
  231.     shr    al,1
  232.     shr    al,1        ;isolate the high nibble of the
  233.                 ; color #
  234.     call    BinToHexDigit    ;convert the high color # nibble
  235.     mov    [bx],al        ; and put it into the text
  236.     pop    ax        ;get back the color #
  237.     push    ax        ;save the color #
  238.     and    al,0fh        ;isolate the low color # nibble
  239.     call    BinToHexDigit    ;convert the low nibble of the
  240.                 ; color # to ASCII
  241.     mov    [bx+1],al    ; and put it into the text
  242.     add    bx,COLOR_ENTRY_LENGTH    ;point to the next entry
  243.     pop    ax        ;get back the color #
  244.     inc    ax        ;next color #
  245.     loop    ColorNumberLoop
  246.     mov    ah,9        ;DOS print string function
  247.     mov    dx,offset ColorNumbers
  248.     int    21h        ;put up the attribute numbers
  249.     ret
  250. ColorNumbersUp    endp
  251. ;
  252. Start    endp
  253. Code    ends
  254.     end    Start
  255.  
  256.  
  257.